package org.benjamin.lending.trusteeship.sumapay;

import org.apache.commons.lang3.ObjectUtils;
import org.benjamin.lending.domain.entity.BorrowInfo;
import org.benjamin.lending.domain.entity.MemberInfo;
import org.benjamin.lending.domain.entity.TrusteeshipRequest;
import org.benjamin.lending.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Created by xywen on 2017/5/26.
 */
@Component("sumapayRealNameAuthOperation")
public class RealNameAuthOperation extends SumapayAbstractOperation {

    @Autowired
    private MemberService memberService;

    @Override
    protected void processRequest(Map<String, Object> requestMap, MemberInfo memberInfo, BorrowInfo borrowInfo, Map<String, Object> parameterMap) {
        String realName = parameterMap.get("realName").toString();
        String idNumber = parameterMap.get("idNumber").toString();
        requestMap.put("userName",realName);
        requestMap.put("idNumber",idNumber);
        asynchronous = true;
        this.abutmentUrl = sumaplayConfig.getServiceName()+"/main/UserForFT_realNameAuth";
    }

    @Override
    protected void buildAbutmentUrl(Map<String, Object> requestMap) {
        //super.buildAbutmentUrl(requestMap);
    }

    @Override
    protected String signatureRequest(Map<String, Object> requestMap) {
        String[] signatures = this.buildSignature(requestMap,"requestId","merchantCode","userName","idNumber","payType","noticeUrl","merBizRequestId");
        requestMap.put("signature",signatures[1]);
        return signatures[0];
    }

    @Override
    protected void afterRequest(Map<String, Object> parameterMap, MemberInfo memberInfo,BorrowInfo borrowInfo, TrusteeshipRequest trusteeshipRequest) {

    }

    @Override
    protected String[] signatureResponse(Map<String, Object> responseMap) {
        return this.buildSignature(responseMap,"requestId","result","userName","status","token","idNumber");
    }

    @Override
    protected void processResponse(TrusteeshipRequest trusteeshipRequest, Map<String, Object> responseMap) throws RuntimeException {
        Integer userId = trusteeshipRequest.getUid();
        String realName = responseMap.get("userName").toString();
        String idNumber = responseMap.get("idNumber").toString();
        String token = responseMap.get("token").toString();
        String pictureData = ObjectUtils.toString(responseMap.get("pictureData"));
        this.memberService.memberIdentitySuccess(userId,realName,idNumber,token,pictureData);
        super.processResponse(trusteeshipRequest, responseMap);
    }
}
